package com.sms.graduate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sms.graduate.dao.SmsClasscourseDao;
import com.sms.graduate.entity.SmsClasscourse;
import com.sms.graduate.entity.dto.CourseDTO;
import com.sms.graduate.service.SmsClasscourseService;
import com.sms.graduate.utils.ConstantUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class SmsClasscourseServiceImpl extends ServiceImpl<SmsClasscourseDao, SmsClasscourse> implements SmsClasscourseService {

    @Override
    public List<SmsClasscourse> courseByClass(String courseClass, String courseTime) {
        QueryWrapper<SmsClasscourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SmsClasscourse.COL_COURSECLASS,courseClass)
                .eq(SmsClasscourse.COL_COURSETIME,courseTime);
        List<SmsClasscourse> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public Boolean updateCourse(String dayWeek, String courseTime, String course,String courseClass) {
        UpdateWrapper<SmsClasscourse> wrapper = new UpdateWrapper<>();
        wrapper.set(SmsClasscourse.COL_COURSE,course)
                .eq(SmsClasscourse.COL_DAYWEEK,dayWeek)
                .eq(SmsClasscourse.COL_COURSECLASS,courseClass)
                .eq(SmsClasscourse.COL_COURSETIME,courseTime);
        return this.update(wrapper);
    }

    @Override
    public Boolean addCourse(String dayWeek, String courseTime, String course,String courseClass) {
        SmsClasscourse smsClasscourse = new SmsClasscourse();
        smsClasscourse.setCourse(course);
        smsClasscourse.setDayWeek(dayWeek);
        smsClasscourse.setCourseTime(courseTime);
        smsClasscourse.setCourseClass(courseClass);
        return this.save(smsClasscourse);
    }

    @Override
    public Boolean selectCourse(String dayWeek, String courseTime,String courseClass) {
        QueryWrapper<SmsClasscourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SmsClasscourse.COL_DAYWEEK,dayWeek)
                .eq(SmsClasscourse.COL_COURSECLASS,courseClass)
                .eq(SmsClasscourse.COL_COURSETIME,courseTime);
        SmsClasscourse one = this.getOne(queryWrapper);
        if (one == null){
            return false;
        }
        return true;
    }

    @Override
    public void updateAndAddCourse(CourseDTO courseDTO, String courseClass,String dayWeek) {
        List<String> list = new ArrayList<>();
        list.add(courseDTO.getTime1());
        list.add(courseDTO.getTime2());
        list.add(courseDTO.getTime3());
        list.add(courseDTO.getTime4());
        list.add(courseDTO.getTime5());
        list.add(courseDTO.getTime6());
        list.add(courseDTO.getTime7());
        list.add(courseDTO.getTime8());
        for (int i = 1; i <=list.size(); i++) {
            String s = list.get(i-1);
                if (i == 1){
                    if (selectCourse(dayWeek,ConstantUtils.a,courseClass)){
                          updateCourse(dayWeek,ConstantUtils.a,s,courseClass);
                    }else {
                        addCourse(dayWeek,ConstantUtils.a,s,courseClass);
                    }

                }
                if (i == 2){
                    if (selectCourse(dayWeek,ConstantUtils.b,courseClass)){
                        updateCourse(dayWeek,ConstantUtils.b,s,courseClass);
                    }else {
                        addCourse(dayWeek,ConstantUtils.b,s,courseClass);
                    }
                }
                if (i == 3){
                    if (selectCourse(dayWeek,ConstantUtils.c,courseClass)){
                        updateCourse(dayWeek,ConstantUtils.c,s,courseClass);
                    }else {
                        addCourse(dayWeek,ConstantUtils.c,s,courseClass);
                    }

                }
                if (i == 4){
                    if (selectCourse(dayWeek,ConstantUtils.d,courseClass)){
                        updateCourse(dayWeek,ConstantUtils.d,s,courseClass);
                    }else {
                        addCourse(dayWeek,ConstantUtils.d,s,courseClass);
                    }
                }
                if (i == 5){
                    if (selectCourse(dayWeek,ConstantUtils.e,courseClass)){
                        updateCourse(dayWeek,ConstantUtils.e,s,courseClass);
                    }else {
                        addCourse(dayWeek,ConstantUtils.e,s,courseClass);
                    }
                }
                if (i == 6){
                    if (selectCourse(dayWeek,ConstantUtils.f,courseClass)){
                        updateCourse(dayWeek,ConstantUtils.f,s,courseClass);
                    }else {
                        addCourse(dayWeek,ConstantUtils.f,s,courseClass);
                    }
                }
                if (i == 7){
                    if (selectCourse(dayWeek,ConstantUtils.g,courseClass)){
                        updateCourse(dayWeek,ConstantUtils.g,s,courseClass);
                    } else {
                        addCourse(dayWeek,ConstantUtils.g,s,courseClass);
                    }
                }
                if (i == 8){
                    if (selectCourse(dayWeek,ConstantUtils.h,courseClass)){
                        updateCourse(dayWeek,ConstantUtils.h,s,courseClass);
                    }else {
                        addCourse(dayWeek,ConstantUtils.h,s,courseClass);
                    }
                }
            }
        }
}

